﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;

public partial class Controls_BackEnd_UserFunctions : UserControl
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!CheckTimeOut()) return;
        if (IsPostBack) return;
        BinderData(string.Empty);
    }




    /// <summary>
    /// 
    /// 
    /// </summary>
    /// <param name="Condition"></param>
    private void BinderData(string Condition)
    {
        var functionlist = new Functions().Functions_GetByCondition(Condition);
        var listgrant = (List<Functions>)Session[SessionManager.SessionList];
        var listvalid = functionlist.Where(functionse => !listgrant.Exists(p => p.FunctionID == functionse.FunctionID) && functionse.FatherID != 0 && functionse.IsDisplay && functionse.IsActive).ToList();
        Session[SessionManager.SessionListpopup] = listvalid;
        if (listvalid.Count > 0)
        {
            grvData.DataSource = listvalid;
            grvData.DataBind();
            return;
        }
        Config.NullData(new Functions(), grvData);
    }

    /// <summary>
    /// 
    /// </summary>
    private bool CheckTimeOut()
    {
        if (Session.Count <= 0)
        {
            Config.MessageAndClosePopup(Page, Resources.Resource.TimeOutMsg, Resources.Resource.Warning);
            return false;
        }
        return true;
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        switch (e.Row.RowType)
        {
            case DataControlRowType.Header:
                e.Row.Attributes.Add("style", "background-color:black;color:white;");
                break;
            case DataControlRowType.DataRow:
                foreach (TableCell tableCell in e.Row.Cells)
                    tableCell.Attributes.Add("style", "border:1px solid #D2D2D2;padding:3px 0 10px 10px;background-color:#D2D2D2;");
                break;
            case DataControlRowType.Footer:
                break;
        }
    }







    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void AllCommand(object sender, EventArgs e)
    {
        if (!CheckTimeOut()) return;
        switch (((Control)sender).ID)
        {

            case "btnSearch":
                var Condition = " Name like '%" + txtContent.Text + "%'";
                BinderData(Condition);
                break;
        }

    }


}
